package cn.pcManage.shopManage.service.impl;

import cn.common.common.model.ServerResponse;
import cn.common.shop.model.BaseNoticeInfo;
import cn.pcManage.shopManage.dao.NoticeInfoMapper;
import cn.pcManage.shopManage.service.INoticeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Zhang on 2019/1/29.
 */
@Service
public class NoticeServiceImpl implements INoticeService{
    @Autowired
    NoticeInfoMapper noticeInfoMapper;

    @Override
    public ServerResponse<?> addNotice(BaseNoticeInfo noticeInfo) {
        Integer maxIndex = noticeInfoMapper.getMaxIndex(noticeInfo.getType());
        if (maxIndex == null){
            maxIndex = 1;
        }else {
            maxIndex++;
        }
        noticeInfo.setIndexNum(maxIndex);
        noticeInfo.setStatus("use");
        noticeInfoMapper.insertSelective(noticeInfo);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> updateNotice(BaseNoticeInfo noticeInfo) {
        noticeInfoMapper.updateByPrimaryKeySelective(noticeInfo);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> deleteNotice(Integer id) {
        BaseNoticeInfo baseNoticeInfo = noticeInfoMapper.selectByPrimaryKey(id);
        baseNoticeInfo.setStatus("delete");
        noticeInfoMapper.updateByPrimaryKeySelective(baseNoticeInfo);
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<?> getNoticeList(BaseNoticeInfo noticeInfo) {
        Map<String,Object> resultMap=new HashMap<String, Object>(2);
        PageHelper.startPage(noticeInfo.getPage(),noticeInfo.getLimit());
        List<BaseNoticeInfo> noticeInfos = noticeInfoMapper.getNoticeList(noticeInfo);
        Page<BaseNoticeInfo> noticeInfoPage = (Page<BaseNoticeInfo>) noticeInfos;
        resultMap.put("total", noticeInfoPage.getTotal());
        resultMap.put("infos", noticeInfoPage);
        return ServerResponse.createBySuccess(resultMap);
    }

    @Override
    public ServerResponse<?> uploadImg(MultipartFile multipartFile) {
        String fileName;
        String path = "shop/main";
        try {
            path = "/home/img/" + path + "/";
            //获取上传文件名
            String originalFilename=multipartFile.getOriginalFilename();
            String uploadFileSuffix = originalFilename.substring(
                    originalFilename.lastIndexOf('.'));
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            fileName = UUID.randomUUID() + uploadFileSuffix;
            FileOutputStream out=new FileOutputStream(new File(path+ fileName));
            byte [] bytes=new byte[10240];
            int len;
            FileInputStream in = (FileInputStream) multipartFile.getInputStream();
            while((len=in.read(bytes))!=-1){
                out.write(bytes, 0, len);
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage("上传失败");
        }
        return ServerResponse.createBySuccessMessage(path+fileName);
    }

    @Override
    public ServerResponse<?> changeOrder(Integer id, String type) {
        BaseNoticeInfo baseNoticeInfo = noticeInfoMapper.selectByPrimaryKey(id);
        if ("up".equals(type)){
            if (baseNoticeInfo.getIndexNum() == 1){
                return ServerResponse.createByErrorMessage("已是第一项");
            }else {
                BaseNoticeInfo last = noticeInfoMapper.getInfoByIndex(baseNoticeInfo.getType(),
                        baseNoticeInfo.getIndexNum() - 1);
                if (last!=null){
                    last.setIndexNum(last.getIndexNum() + 1);
                    noticeInfoMapper.updateByPrimaryKeySelective(last);
                }
                baseNoticeInfo.setIndexNum(baseNoticeInfo.getIndexNum() - 1);
                noticeInfoMapper.updateByPrimaryKeySelective(baseNoticeInfo);
            }
        }else {
            if (baseNoticeInfo.getIndexNum().equals(noticeInfoMapper.getLaseIndexByType(baseNoticeInfo.getType()))){
                return ServerResponse.createByErrorMessage("已是最后一项");
            }else {
                BaseNoticeInfo next = noticeInfoMapper.getInfoByIndex(baseNoticeInfo.getType(),
                        baseNoticeInfo.getIndexNum() + 1);
                if (next!=null){
                    next.setIndexNum(next.getIndexNum() - 1);
                    noticeInfoMapper.updateByPrimaryKeySelective(next);
                }
                baseNoticeInfo.setIndexNum(baseNoticeInfo.getIndexNum() + 1);
                noticeInfoMapper.updateByPrimaryKeySelective(baseNoticeInfo);
            }
        }
            return ServerResponse.createBySuccessMessage("操作成功");
    }
}
